import java.util.Arrays;
import java.io.*;

class Anagram {
	
	public static void main(String[] args){
		Anagram anagramTester = new Anagram();

		try{
			String firstAnagramCandidat = anagramTester.
										getAnagramCandidat("Enter first phrase: ");
			String secondAnagramCandidat = anagramTester.
										getAnagramCandidat("Enter second phrase: ");
			
			boolean doesSpecifiedRowsAreAnagram = anagramTester.isAnagram(
										firstAnagramCandidat, 
										secondAnagramCandidat);
						
			if (doesSpecifiedRowsAreAnagram) {
				System.out.println("Provided rows are anagrams");
			} else {
				System.out.println("Provided rows aren't anagrams");
			}
		}
		catch (IOException ex) {
			System.out.println("There was an exception trying to read an anagram candidat...");
		}
	}
	
	public boolean isAnagram(String firstPhrase, String secondPhrase){
		char[] firstPraseConverted = firstPhrase.toCharArray();
		char[] secondPraseConverted = secondPhrase.toCharArray();;
		
		Arrays.sort(firstPraseConverted);
		Arrays.sort(secondPraseConverted);
		return Arrays.equals(firstPraseConverted, secondPraseConverted);
	}

	private String prepareAnagramCandidat(String anagramCandidat){
		return anagramCandidat.
						replaceAll(" ", "").
							toLowerCase();
	}
	
	private String getAnagramCandidat(String consoleMessage) throws IOException{
		String result = new ConsoleReaderUtility().readString(consoleMessage);
		
		return prepareAnagramCandidat(result);
	}

}